<template>
    <div id="input">
        <span @click="minus">-</span>
        <span class="count">{{count}}</span>
        <span @click="add">+</span>
    </div>
</template>

<script>

    export default{
        //需要接受的父组件传过来的的值  初始数量和商品id
        props:['initCount','goodsid'],
        data(){
            return{
                //初始购买数量
                count:1,
                resObj:{goodsid:0,type:'add'}
            }
        },
        created(){
            //这里的initCount是父亲传过来的meassage的cou
            this.count = this.initCount;
        },
        methods:{
            //减
            minus(){
                this.count--;
                if(this.count<1){
                    this.count=0;
                    return;
                }
                this.sendMessage('minus');
            },
            //加
            add(){
                this.count++;
                this.sendMessage('add');
            },
            sendMessage(type){
                this.resObj.type = type;
                this.resObj.goodsid = this.goodsid;
                //再购物车页面修改的话  再把resObj的值传递出去
                this.$emit('cardataObj',this.resObj);

            }

        }

    }
</script>
<style scoped>
    span{
        border: 1px solid #ccc;
        display: inline-block;
        width: 30px;
        height: 30px;
        text-align: center;
        line-height: 30px;
    }
    .count{
        width: 40px;
    }
</style>